package com.ruding.disruptor.utils;


import java.io.*;
import org.apache.pdfbox.exceptions.InvalidPasswordException;
 
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.util.PDFTextStripper;
import org.apache.pdfbox.util.TextPosition;

import java.io.IOException;
import java.util.List;
 
public class PrintTextLocations extends PDFTextStripper {
 
    public PrintTextLocations() throws IOException {
        super.setSortByPosition(true);
    }
 
    public static void main(String[] args) throws Exception {
        PDDocument document = null;
        try {
            File input = new File("C:\\Users\\520878\\Desktop\\CgsMMFyTVK2AFXpdAA0UsiYU4QA689.pdf");
            document = PDDocument.load(input);
            PrintTextLocations printer = new PrintTextLocations();
            List allPages = document.getDocumentCatalog().getAllPages();
            for (int i = 0; i < allPages.size(); i++) {
                PDPage page = (PDPage) allPages.get(i);
                PDStream contents = page.getContents();
                if (contents != null) {
                    printer.processStream(page, page.findResources(), page.getContents().getStream());
                }
            }
        } finally {
            if (document != null) {
                document.close();
            }
        }
    }
 
    /**
     * @param text The text to be processed
     */
    @Override /* this is questionable, not sure if needed... */
    protected void processTextPosition(TextPosition text) {
        System.out.println(text.getCharacter());
    }

}
